//
//  ZYVisitorView.swift
//  SwiftDemo1
//
//  Created by wzp on 2025/6/1.
//

import Foundation
import UIKit

private enum ZYVisitorButtonType: Int {
    case register = 1001
    case login = 1002
}

enum ZYVisitorSource {
    case home
    case message
    case discover
    case profileVc
}

class ZYVisitorView: UIView {
    private var displayView: UIView?
    private var descLabel: UILabel = UILabel()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addDisplayView(_ displayView: UIView, width: CGFloat = 200, height: CGFloat = 200) {
        self.displayView?.removeFromSuperview()
        addSubview(displayView)
        self.displayView = displayView
        
        self.displayView?.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.bottom.equalTo(descLabel.snp_topMargin).offset(-25)
            make.width.equalTo(width)
            make.height.equalTo(height)
        }
    }
    
    func updateDescText(_ descText: String) {
        self.descLabel.text = descText
    }
}

// MARK: - UI && event
extension ZYVisitorView {
    
    private func setupUI() {
        backgroundColor = UIColor.hexColor(0xDDDDDD)
        descLabel.textColor = UIColor.hexColor(0x999999)
        descLabel.font = UIFont.systemFont(ofSize: 12)
        descLabel.text = "关注一些人，回这里看看有什么惊喜"
        descLabel.numberOfLines = 2
        descLabel.textAlignment = .center
        addSubview(descLabel)
        
        addButton("注册", type: .register)
        addButton("登录", type: .login)
        
        descLabel.snp.makeConstraints { make in
            make.left.equalTo(self).offset(100)
            make.right.equalTo(self).offset(-70)
            make.centerY.equalTo(self).offset(100)
        }
    }
    
    private func addButton(_ title: String, type: ZYVisitorButtonType) {
        let btn = UIButton(type: .system)
        btn.adjustsImageWhenDisabled = false
        btn.adjustsImageWhenHighlighted = true
        
        btn.setTitle(title, for: .normal)
        let textColor = type == .register ? UIColor.orange : UIColor.hexColor(0x999999)
        btn.setTitleColor(textColor, for: .normal)
        btn.tag = type.rawValue
        btn.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        btn.backgroundColor = .white
        addSubview(btn)
        
        let height = 28
        let marginY = 140
        if type == ZYVisitorButtonType.register {
            btn.snp.makeConstraints { make in
                make.left.equalTo(100)
                make.centerY.equalTo(self).offset(marginY)
                make.width.equalTo(70)
                make.height.equalTo(height)
            }
        }
        else {
            btn.snp.makeConstraints { make in
                make.right.equalTo(-100)
                make.centerY.equalTo(self).offset(marginY)
                make.width.equalTo(70)
                make.height.equalTo(height)
            }
        }
    }
    
    @objc private func buttonTapped(_ sender: UIButton) {
        
    }
}

